﻿#include "cprogressdlg.h"
#include "ui_cprogressdlg.h"
#include <QFont>
#include <QLabel>
#include <QProgressBar>
#include <QPushButton>
#include <QGridLayout>
#include<QHBoxLayout>
#include <QProgressDialog>
#include "mytestsignal.h"
#include<QTime>
#include<QDateTime>
#include<QApplication>

#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif//将乱码转为中文



void CProgressDlg::BeginLayout()
{
            QFont font("ZYSong18030", 9);
            setFont(font);
            setWindowTitle(tr("进度显示"));

            pM_edit1=new QLabel;
            pM_edit1->setText(tr("已完成："));
            pM_edit1->setAlignment(Qt::AlignLeft);

            pM_edit2=new QLabel;
            pM_edit2->setText(tr("已用时："));
            pM_edit2->setAlignment(Qt::AlignLeft);

            pM_edit3=new QLabel;
            pM_edit3->setText(tr("状态："));
            pM_edit3->setAlignment(Qt::AlignLeft);

            pM_data1=new QLabel;
            pM_data1->setAlignment(Qt::AlignLeft);
            pM_data2=new QLabel;

            pM_data3=new QLabel;

            pM_data4=new QLabel;

            pM_progressBar = new QProgressBar;

            pM_cancelbtn = new QPushButton;
            pM_cancelbtn->setText(tr("取消"));
            pM_cancelbtn->setFixedSize(70,40);

           pM_mainLayout=new QGridLayout(this);
           pM_mainLayout->addWidget(pM_edit1,2,0);
           pM_mainLayout->addWidget(pM_data1,2,1);
           pM_mainLayout->setColumnStretch(2,2);
           pM_mainLayout->addWidget(pM_edit2,2,4);
           pM_mainLayout->addWidget(pM_data2,2,5);
           pM_mainLayout->setColumnStretch(6,1);
           pM_mainLayout->addWidget(pM_edit3,3,0);
           pM_mainLayout->addWidget(pM_data3,3,1);
           pM_mainLayout->setRowStretch(4,1);
           pM_mainLayout->addWidget(pM_progressBar,5,0,1,7);
           pM_mainLayout->addWidget(pM_data4,6,1);
           pM_mainLayout->addWidget(pM_cancelbtn,7,6);
           this->resize(40,20);
           pM_mainLayout->setMargin(30);
           pM_mainLayout->setSpacing(10);
}

void CProgressDlg::AfterLayout()
{ if(m_sTask.size()>0)//存在一个任务框时
    {
        QLabel*v_task = new QLabel;
        v_task->setText(m_sTask.at(0));
        v_task->setAlignment(Qt::AlignCenter);
        v_task->setMinimumSize( 150, 75);
        v_task->setStyleSheet("QLabel { background-color : gray; color : black; border-radius:15px}");
        mLabels.push_back(v_task);

        pM_HmainLayout = new QHBoxLayout(this);
        pM_HmainLayout->setDirection(QBoxLayout::LeftToRight);
        pM_HmainLayout->addWidget(v_task, 1,Qt::AlignCenter);
        pM_mainLayout->setColumnStretch( 3, 1);
        pM_mainLayout->setRowStretch( 1, 1);
        pM_mainLayout->setColumnStretch( 1, 2*m_sTask.size()-1);

        pM_mainLayout->addLayout(pM_HmainLayout,0,2,Qt::AlignCenter);//嵌入水平布局
    }

    if((m_sTask.size()>0)&&(m_sTask.size()!=1))
    {
       for (int i=1;i<m_sTask.size(); ++i)//存在多个任务框时
        {
           QLabel* v_arrow=new QLabel;
            v_arrow->setPixmap(QPixmap(":/C:/Users/23520/Desktop/svg/arrow/gray.png"));//urr未传送时,将箭头设为灰色
            v_arrow->setScaledContents(true);
            mLabels.push_back(v_arrow);

            QLabel*v_task = new QLabel;
            v_task->setText(m_sTask.at(i));
            v_task->setAlignment(Qt::AlignCenter);
            v_task->setMinimumSize(150,75);
            v_task->setStyleSheet("QLabel { background-color : gray; color : black; border-radius:15px;}");//设置样式表
            mLabels.push_back(v_task);

        pM_HmainLayout->setDirection(QBoxLayout::LeftToRight);
        pM_HmainLayout->addWidget(v_arrow, 1,Qt::AlignCenter);
        pM_HmainLayout->addWidget(v_task, 1,Qt::AlignCenter);
        v_task->setAlignment(Qt::AlignCenter);

         }

       pM_mainLayout->addLayout(pM_HmainLayout,0,2,Qt::AlignCenter);

    }
}

CProgressDlg::CProgressDlg(const QStringList task,QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::CProgressDlg)
{
    ui->setupUi(this);
    for (int i=0;i<task.size();i++)//将任务框名称赋值到Task
    {
        QString temp=task.at(i);
        m_sTask.append(temp);
    }

  this->BeginLayout();

  this->AfterLayout();

    QObject::connect(CProgressDlg::pM_cancelbtn,SIGNAL(clicked()),this,SLOT(SetCancelBtn()));
}

CProgressDlg::~CProgressDlg()
{
    delete ui;
    if(pM_edit1) delete pM_edit1;
    if(pM_edit2) delete pM_edit2;
    if(pM_edit3) delete pM_edit3;
    if(pM_data1) delete pM_data1;
    if(pM_data2) delete pM_data2;
    if(pM_data3) delete pM_data3;
    if(pM_data4) delete pM_data4;
    if(pM_cancelbtn) delete pM_cancelbtn;
    if(pM_progressBar) delete pM_progressBar;
    if(pM_mainLayout) delete pM_mainLayout;
    if(pM_HmainLayout)delete pM_HmainLayout;
}

void CProgressDlg::ProgressTask(int step,int max,int curr)
{
    if (step == 1 )
    {
        if(curr>1 && curr < max)//curr正在传送时,将任务框1设为浅绿色
        {
            mLabels.at(2*step-2)->setStyleSheet("QLabel { background-color : lightgreen; color : black;border-radius:15px; }");
        }
        if(curr == max)//curr传送完后,将任务框1设为深绿色
        {
            mLabels.at(2*step-2)->setStyleSheet("QLabel { background-color : darkgreen; color : black;border-radius:15px; }");
        }
        return;
    }
    if(step>1)
    {
        //任务框1完成后,将其后面的箭头1设为浅绿色
      mLabels.at(2*step-3)->setPixmap(QPixmap(":/C:/Users/23520/Desktop/svg/arrow/lightgreen.png"));

     if(curr>1 && curr < max)//curr正在传送时,将任务框2设为浅绿色
     {
       mLabels.at(2*step-2)->setStyleSheet("QLabel { background-color : lightgreen; color : black;border-radius:15px; }");
     }

      if(curr == max)//curr传送完后,将箭头1设为深绿色,任务框2设为深绿色
       {
          mLabels.at(2*step-3)->setPixmap(QPixmap(":/C:/Users/23520/Desktop/svg/arrow/darkgreen.png"));

          mLabels.at(2*step-2)->setStyleSheet("QLabel { background-color : darkgreen; color : black;border-radius:15px; }");
       }

            return;
    }
}

void CProgressDlg::SetData1(int step,int max,int curr)
{
    double value=CProgressDlg::pM_progressBar->value();
    value/=max;
    value*=100;
    QString str=QString::number(value);
    str.append("%");
    pM_data1->setText(str);
    return;
}
void CProgressDlg::SetData2()
{
    QTime current=QTime::currentTime();
     int t = this->m_BaseTime.msecsTo(current);
    QTime showTime(0,0,0);
    showTime =showTime.addMSecs(t);
    m_sShowStr = showTime.toString("hh:mm:ss");
    pM_data2->setText(m_sShowStr);
    return;
}
void CProgressDlg::SetData3(QString P_str)
{
    pM_data3->setText(P_str);
    return;
}
void CProgressDlg::SetData4(QString A_str)
{
    pM_data4->setText(A_str);
    return;
}
void CProgressDlg::ProgressBar(int step,int max,int curr)
{
    pM_progressBar->setRange(0, max);
    pM_progressBar->setValue(curr);
    pM_progressBar->setTextVisible(false);//隐藏进度条的百分比
    return;
}
void CProgressDlg::SetCancelBtn()
{
    QApplication::exit();
}

